using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;
using Newtonsoft.Json;
using System.Net.NetworkInformation;

public class LoadingBuriedPoint : MonoBehaviour
{
    public static LoadingBuriedPoint loadingBuriedPoint;
    public void Awake()
    {
        loadingBuriedPoint = this;
    }
    WWW www = null;
    private string DownLoadUrl = "";

    private AndroidJavaObject _appInfoTool;
    public class PlayerData
    {
        public int code;
        public string msg;
        public JsonData data;
        public bool success;

    }
    public class JsonData
    {
        public string AndroidID;
        public string AdjustID;
        public string network;
        public string time;
        public string event1;
        public string ip;
        public string type;
    }
   
    public void OnInitData(int val)
    {
        JsonData data = new JsonData();
        (string adjustid_99, string androidid_99) = GetAdjustidAndGetAndroidId();
        data.AndroidID = androidid_99;

        data.AdjustID = adjustid_99;

        (string network_99, string ip_99) = GetNetworkstatus();
        data.network = network_99;

        DateTime currentTime = DateTime.UtcNow;
        data.time = currentTime.ToString("yyyy-MM-dd HH:mm:ss");

        data.event1 = "dowload";

        data.ip = ip_99;

        data.type = val.ToString();

        Debug.Log("=======json==========");
        string json = JsonConvert.SerializeObject(data);
        Debug.Log(json);
        StartCoroutine(I_RequestByJsonBodyPost("https://game-admin-test.hindupay.net/adjust/inputText", json));
    }                
    private IEnumerator I_RequestByJsonBodyPost(string url, string json)
    {
        Debug.Log("=======json==========" + json+"   =====URL===="+ url);
        UnityWebRequest www = UnityWebRequest.Post(url, json);
        www.SetRequestHeader("Content-Type", "application/json;charset=utf-8");
        byte[] bodyRaw = Encoding.UTF8.GetBytes(json);
        www.uploadHandler = new UploadHandlerRaw(bodyRaw);
        //yield return 0;
        yield return www.SendWebRequest();
        Debug.Log(www.downloadHandler.text);
        // PlayerData player = JsonUtility.FromJson<PlayerData>(www.downloadHandler.text);
        // Debug.Log("Player code: " + player.code);
        // Debug.Log("Player packName: ");
        // //GameInit.SetActive(true);
        // if (player.code == 0)
        // {
        //     Debug.Log("Player code: " + player.code);
        // }
    }

   public Tuple<string, string> GetNetworkstatus()
    {
        // 获取所有的网络接口
        NetworkInterface[] networkInterfaces = NetworkInterface.GetAllNetworkInterfaces();

        foreach (NetworkInterface networkInterface in networkInterfaces)
        {
            // 如果接口状态为Up，则输出更多信息
            if (networkInterface.OperationalStatus == OperationalStatus.Up)
            {
                // 判断接口类型是否为Ethernet或WiFi
                if (networkInterface.NetworkInterfaceType == NetworkInterfaceType.Ethernet ||
                    networkInterface.NetworkInterfaceType == NetworkInterfaceType.Wireless80211)
                {
                    // 获取连接类型和 IP 地址
                    string connectionType = GetNetworkConnectionType(networkInterface.NetworkInterfaceType);
                    string ipAddress = GetIPAddress(networkInterface);

                    return Tuple.Create(connectionType, ipAddress);
                }
                // 检查描述以确定移动数据连接
                else if (networkInterface.Description.Contains("Mobile") ||
                        networkInterface.Description.Contains("Cellular") ||
                        networkInterface.Description.Contains("3G") ||
                        networkInterface.Description.Contains("4G"))
                {
                    return Tuple.Create("移动数据", "无法获取 IP 地址");
                }
            }
        }

        // 如果没有匹配的网络连接类型，则返回默认值
        return Tuple.Create("未知", "无法获取 IP 地址");
    }

    // 根据网络接口类型返回相应的连接类型
    static string GetNetworkConnectionType(NetworkInterfaceType networkInterfaceType)
    {
        switch (networkInterfaceType)
        {
            case NetworkInterfaceType.Ethernet:
                return "有线网络";
            case NetworkInterfaceType.Wireless80211:
                return "WiFi";
            default:
                return "未知";
        }
    }
    // 获取网络接口的 IP 地址
    private string GetIPAddress(NetworkInterface networkInterface)
    {
        IPInterfaceProperties properties = networkInterface.GetIPProperties();
        foreach (UnicastIPAddressInformation ip in properties.UnicastAddresses)
        {
            // 排除IPv6地址
            if (ip.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
            {
                return ip.Address.ToString();
            }
        }
        return "无法获取 IP 地址";
    }
    
    public Tuple<string, string> GetAdjustidAndGetAndroidId()
    {
        #if !UNITY_EDITOR && UNITY_ANDROID
            _appInfoTool = new AndroidJavaObject("com.amew.xumyrich.SJ");
            string adjustid = _appInfoTool.Call<string>("GetAdjustid");
            string androidid = SystemInfo.deviceUniqueIdentifier;
            return Tuple.Create(adjustid, androidid);
        #elif UNITY_WEBGL
            return Tuple.Create(UnityEngine.Random.Range(10000, 90000000000).ToString(), "Not available in editor or non-Android platforms");;
        #endif
        return Tuple.Create("", "");
    }

}
